package MEET.src.View;

import MEET.src.code.*;

import javax.swing.*;
import java.awt.*;

public class main extends JFrame {
    private String currentUserID; // 保存当前登录用户ID

    public main(String username) {
        this.currentUserID = username; // 保存用户名
        initComponents();
        // 按钮事件监听
        addEventListeners();
    }

    private void addEventListeners() {
        // "我的预订"按钮事件
        button2.addActionListener(e -> showUserReservations());
        //"空闲会议室"按钮事件
        button3.addActionListener(e -> new FreeMeetingRooms().setVisible(true));
        // "我的信息"按钮事件
        button4.addActionListener(e -> showUserInfo());
        // "退出系统"按钮事件
        button5.addActionListener(e -> this.dispose());
        //“会议申请”按钮事件
        button6.addActionListener(e -> new Conference().setVisible(true));
        //“会议查询”按钮事件
        button7.addActionListener(e -> new MeetingQueryForm1().setVisible(true));
        // "会议审核"按钮事件（管理员权限）
        button8.addActionListener(e -> {
            String power = UserDao.getUserPowerById(currentUserID);
            if ("管理员".equals(power)) {
                new Aduit().setVisible(true); // 创建并显示审核会议窗口
            } else {
                JOptionPane.showMessageDialog(this,
                        "您没有权限进行会议审核！",
                        "权限不足", JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    private void showUserInfo() {
        // 获取用户信息并显示
        User user = UserDao.getUserInfo(currentUserID);
        if (user != null) {
            new UserInfoFrame(user).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "无法获取用户信息", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showUserReservations() {
        // 显示用户的预订
        new UserReservationsFrame(currentUserID).setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();
        button8 = new JButton();
        label1 = new JLabel();

        //======== this ========
        setTitle("\u4e0a\u6d77\u7535\u529b\u5927\u5b66\u4f1a\u8bae\u9884\u8ba2\u7cfb\u7edf");
        setBackground(new Color(0x99ffff));
        setForeground(SystemColor.menu);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button1 ----
        button1.setText("\u9996  \u9875");
        button1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        button1.setBackground(Color.white);
        contentPane.add(button1);
        button1.setBounds(0, 0, 120, 45);

        //---- button2 ----
        button2.setText("\u6211\u7684\u9884\u8ba2");
        button2.setBackground(Color.white);
        contentPane.add(button2);
        button2.setBounds(120, 0, 100, 45);

        //---- button3 ----
        button3.setText("\u7a7a\u95f2\u4f1a\u8bae\u5ba4");
        button3.setBackground(Color.white);
        contentPane.add(button3);
        button3.setBounds(215, 0, 100, 45);

        //---- button4 ----
        button4.setText("\u6211\u7684\u4fe1\u606f");
        button4.setBackground(Color.white);
        contentPane.add(button4);
        button4.setBounds(315, 0, 100, 45);

        //---- button5 ----
        button5.setText("\u9000\u51fa\u7cfb\u7edf");
        button5.setBackground(Color.white);
        contentPane.add(button5);
        button5.setBounds(415, 0, 100, 45);

        //---- button6 ----
        button6.setText("\u9884\u8ba2\u7533\u8bf7");
        button6.setBackground(Color.white);
        button6.setFont(new Font("Microsoft JhengHei UI", Font.BOLD, 30));
        contentPane.add(button6);
        button6.setBounds(75, 200, 175, 110);

        //---- button7 ----
        button7.setText("\u4f1a\u8bae\u67e5\u8be2");
        button7.setBackground(Color.white);
        button7.setFont(new Font("Microsoft JhengHei UI", Font.BOLD, 30));
        contentPane.add(button7);
        button7.setBounds(330, 200, 185, 110);

        //---- button8 ----
        button8.setText("\u5ba1\u6838\u4f1a\u8bae");
        button8.setBackground(Color.white);
        button8.setFont(new Font("Microsoft JhengHei UI", Font.BOLD, 30));
        contentPane.add(button8);
        button8.setBounds(580, 200, 185, 110);

        //---- label1 ----
        label1.setText("text");
        label1.setIcon(new ImageIcon(getClass().getResource("/MEET/src/Main/resourse/\u6821\u95e8.jpg")));
        contentPane.add(label1);
        label1.setBounds(0, -5, 865, 525);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(865, 550);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
